/*
 * Wrappers around mutex/cond/thread functions
 *
 * Copyright Red Hat, Inc. 2009
 *
 * Author:
 *  Marcelo Tosatti <mtosatti@redhat.com>
 *
 * This work is licensed under the terms of the GNU GPL, version 2 or later.
 * See the COPYING file in the top-level directory.
 *
 */
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <time.h>
#include <signal.h>
#include <stdint.h>
#include <string.h>
#include "qemu-thread.h"

static void error_exit(int err, const char *msg) {
    fprintf(stderr, "qemu: %s: %s\n", msg, strerror(err));
    abort();
}

void qemu_mutex_init(QemuMutex *mutex) {
    int err;
    pthread_mutexattr_t mutexattr;

    LOGD_TRD("%s1:%d\n", __func__, mutex);

    pthread_mutexattr_init(&mutexattr);
    pthread_mutexattr_settype(&mutexattr, PTHREAD_MUTEX_ERRORCHECK);
    err = pthread_mutex_init(&mutex->lock, &mutexattr);
    pthread_mutexattr_destroy(&mutexattr);
    if (err) {
        error_exit(err, __func__);
    }
}

void qemu_mutex_destroy(QemuMutex *mutex) {
    int err;

    err = pthread_mutex_destroy(&mutex->lock);
    if (err)
        error_exit(err, __func__);
}

void qemu_mutex_lock(QemuMutex *mutex) {
    int err;
    LOGD_TRD("%s1:%d\n", __func__, mutex);
    err = pthread_mutex_lock(&mutex->lock);
    LOGD_TRD("%s2\n", __func__);
    if (err) {
        LOGD_TRD("%s3\n", __func__);
        error_exit(err, __func__);
        LOGD_TRD("%s4\n", __func__);
    }
}

int qemu_mutex_trylock(QemuMutex *mutex) {
    return pthread_mutex_trylock(&mutex->lock);
}

void qemu_mutex_unlock(QemuMutex *mutex) {
    int err;
    LOGD_TRD("%s1:%d\n", __func__, mutex);
    err = pthread_mutex_unlock(&mutex->lock);
    LOGD_TRD("%s2:%d\n", __func__, mutex);
    if (err) {
        LOGD_TRD("%s3:%d\n", __func__, mutex);
        error_exit(err, __func__);
        LOGD_TRD("%s4:%d\n", __func__, mutex);
    }
}

void qemu_cond_init(QemuCond *cond) {
    int err;

    err = pthread_cond_init(&cond->cond, NULL);
    if (err)
        error_exit(err, __func__);
}

void qemu_cond_destroy(QemuCond *cond) {
    int err;

    err = pthread_cond_destroy(&cond->cond);
    if (err)
        error_exit(err, __func__);
}

void qemu_cond_signal(QemuCond *cond) {
    int err;

    err = pthread_cond_signal(&cond->cond);
    if (err)
        error_exit(err, __func__);
}

void qemu_cond_broadcast(QemuCond *cond) {
    int err;

    err = pthread_cond_broadcast(&cond->cond);
    if (err)
        error_exit(err, __func__);
}

void qemu_cond_wait(QemuCond *cond, QemuMutex *mutex) {
    int err;

    err = pthread_cond_wait(&cond->cond, &mutex->lock);
    if (err)
        error_exit(err, __func__);
}

void qemu_thread_create(QemuThread *thread,
        void *(*start_routine)(void*),
        void *arg, int mode) {
    sigset_t set, oldset;
    int err;
    pthread_attr_t attr;

    err = pthread_attr_init(&attr);
    if (err) {
        error_exit(err, __func__);
    }
    if (mode == QEMU_THREAD_DETACHED) {
        err = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
        if (err) {
            error_exit(err, __func__);
        }
    }

    // MK - Setting high priority based on FIFO

    /*
#ifdef __ANDROID__
	int rt_max_prio, rt_min_prio;
	struct sched_param rt_param;
	pthread_attr_init(&attr);
	pthread_attr_setschedpolicy(&attr, SCHED_FIFO);
	rt_max_prio = sched_get_priority_max(SCHED_FIFO);
	rt_min_prio = sched_get_priority_min(SCHED_FIFO);
	printf("Thread Priority Range: %d ... %d\n", rt_min_prio, rt_max_prio);
	rt_param.sched_priority = rt_max_prio;
	printf("Setting Priority for qemu_thread_create(): %d\n",rt_param.sched_priority);
	pthread_attr_setschedparam(&attr, &rt_param);
#endif // MK
	*/


    /* Leave signal handling to the iothread.  */
    sigfillset(&set);
    pthread_sigmask(SIG_SETMASK, &set, &oldset);
    err = pthread_create(&thread->thread, &attr, start_routine, arg);
    if (err)
        error_exit(err, __func__);

    pthread_sigmask(SIG_SETMASK, &oldset, NULL);

    pthread_attr_destroy(&attr);
}


void qemu_thread_get_self(QemuThread *thread) {
    thread->thread = pthread_self();
}

int qemu_thread_is_self(QemuThread *thread) {
    return pthread_equal(pthread_self(), thread->thread);
}

void qemu_thread_exit(void *retval) {
    pthread_exit(retval);
}

void *qemu_thread_join(QemuThread *thread) {
    int err;
    void *ret;

    err = pthread_join(thread->thread, &ret);
    if (err) {
        error_exit(err, __func__);
    }
    return ret;
}
